package com.pv.bfc.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

public class GreetingController extends AbstractController {

	/**
	 * Stores the counter value. This will only work when the controller is a
	 * singleton. The application context should be used to make this more
	 * solid.
	 */
	private static int counter = 0;
	/**
	 * The parameter string that is used to retrieve the greeting message from
	 * the request.
	 */
	private static final String PARAM_MSG = "message";

	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest req,
			HttpServletResponse resp) throws Exception {

		String message = ServletRequestUtils.getRequiredStringParameter(req,
				PARAM_MSG);
		increaseCounter();
		ModelAndView mav = new ModelAndView("greetings");
		mav.addObject(PARAM_MSG, message);
		mav.addObject("counter", counter);
		return mav;
	}

	/**
	 * A very simplistic counter implementation.
	 */
	private void increaseCounter() {
		counter++;
	}
}
